﻿using C1.WPF.TileView;
using Model.Modules;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViewModel;
using Utility;
using System.IO;
using System.Globalization;

namespace SteelDetection.UIControl
{
    /// <summary>
    /// PageSteelDetectOL.xaml 的交互逻辑
    /// </summary>
    public partial class UCSteelDetect : UserControl
    {

        UCImgViewer maxviewer = new UCImgViewer();
        public UCSteelDetect()
        {
            InitializeComponent();


            //Loaded += UCSteelDetect_Loaded;
        }


        private void UCSteelDetect_Loaded(object sender, RoutedEventArgs e)
        {
            Logger.Info("StartLoad");
            if (Currentlist == null)
                return;
            Logger.Info(Currentlist.Count.ToString());
            tileview.Items.Clear();
            try
            {
                foreach (var item in Currentlist)
                {
                    C1TileViewItem tileViewItem = new C1TileViewItem();
                    Image image = new Image();
                    //image.SetBinding(Image.SourceProperty, new Binding("Image.image_url"));
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.UriSource = new Uri(item.Image.image_url);
                    bitmap.EndInit();
                    image.Source = bitmap;

                    TextBlock tb = new TextBlock();
                    tb.Text = "sdfdsfsdf";
                    //tb.SetBinding(TextBlock.TextProperty, new Binding("Image.image_url"));

                    UCImgViewer viewer = maxviewer;

                    viewer.ImageInfo = item;
                    tileViewItem.ContentMaximized = viewer;

                    tileViewItem.MinWidth = 100;
                    tileViewItem.MinHeight = 100;
                    tileViewItem.Content = image;
                    tileViewItem.ContentMinimized = image;

                    tileViewItem.MouseEnter += TileViewItem_MouseEnter;
                    tileview.Items.Add(tileViewItem);
                }
            }
            catch(Exception ee) {
                Logger.Error(ee);
            }

           

        }

        private void TileViewItem_MouseEnter(object sender, MouseEventArgs e)
        {
            //var viewer=((sender as C1TileViewItem).Content as UCImgViewer).Viewer;
            //CurrentImg = ((sender as C1TileViewItem).Content as UCImgViewer).ImageInfo;
        }

        public ObservableCollection<Imagedefectinfo> Currentlist
        {
            get {
                return (ObservableCollection<Imagedefectinfo>)GetValue(CurrentlistProperty); 
            }
            set { 
                SetValue(CurrentlistProperty, value);
                
            }
        }

        public static readonly DependencyProperty CurrentlistProperty = DependencyProperty.RegisterAttached("Currentlist", typeof(ObservableCollection<Imagedefectinfo>), typeof(UCSteelDetect), new FrameworkPropertyMetadata(null));


        public int Column
        {
            get
            {
                return (int)GetValue(ColumnProperty);
            }
            set
            {
                SetValue(ColumnProperty, value);
            }
        }
        public static readonly DependencyProperty ColumnProperty = DependencyProperty.RegisterAttached("Column", typeof(int), typeof(UCSteelDetect), new FrameworkPropertyMetadata(3, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


        public int Row
        {
            get
            {
                return (int)GetValue(RowProperty);
            }
            set
            {
                SetValue(RowProperty, value);
            }
        }
        public static readonly DependencyProperty RowProperty = DependencyProperty.RegisterAttached("Row", typeof(int), typeof(UCSteelDetect), new FrameworkPropertyMetadata(1,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public Imagedefectinfo CurrentImg
        {
            get
            {
                return (Imagedefectinfo)GetValue(CurrentImgProperty);
            }
            set
            {
                SetValue(CurrentImgProperty, value);
            }
        }
        public static readonly DependencyProperty CurrentImgProperty = DependencyProperty.RegisterAttached("CurrentImg", typeof(Imagedefectinfo), typeof(UCSteelDetect), new FrameworkPropertyMetadata(null));

        
    }
    public class ImageConvert : IValueConverter
    {
        /// <summary>
        /// 转换图片（解决图片被占用问题）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BitmapImage bitmapImage = null;
            try
            {
                using (BinaryReader reader = new BinaryReader(File.Open(value.ToString(), FileMode.Open)))
            {

                    FileInfo fi = new FileInfo(value.ToString());
                    byte[] bytes = reader.ReadBytes((int)fi.Length);
                    reader.Close();

                    bitmapImage = new BitmapImage();
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

                    bitmapImage.BeginInit();
                    bitmapImage.StreamSource = new MemoryStream(bytes);
                    bitmapImage.EndInit();
                }
                
            }
            catch (Exception) { }
            return bitmapImage;
        }



        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }


    }
}
